'''
Copyright 2013 Matthew Ramina

This file is part of The Time Machine.

    The Time Machine is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    The Time Machine is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with The Time Machine.  If not, see <http://www.gnu.org/licenses/>.
'''
import os, glob, re, csv

class Game(object):
    def __init__(self):
        self.intro = """
        You go to room 140 during lunch and notice a cute door above the room. You climb the lockers, open the door and enter. You see high tech shit and monitors and something covered in a drape. What is it?
        
        You uncover it. This thing is labeled \"Time Machine\". Next to it is a keypad, with what looks like a time and coordinates written on its display. It says \"01/01/1970 0000, 00.0000, 00.0000\".
        
        Which time/place would you like to visit?
        """
        self.questions = []

    def searchText(self):
        self.coordList = []
        self.fileList = []
        for infile in glob.glob(os.path.join('games/', "*.txt")):
            file = open(infile, "r")
            for line in file.readlines():
                if "coords" in line:
                    coords = re.findall('"([^"]*)"', line)
                    self.fileList.append(infile)
                    break
            file.close()
            self.coordList.append(coords[0])
        self.rooms = {}
        return self.coordList

    def parser(self, command):
        command = command.lower()
        parseList = {1:["a", "1"],
                   2:["b", "2"],
                   3:["c", "3"],
                   4:["d", "4"]}
        # k is key, v is list, i is item in list
        for (k, v) in parseList.items():
            for i in v:
                if i == command:
                    return k

    def nextCommand(self, command):
        if len(self.questions) == 0:
            command = int(command)
            file = open(self.fileList[command - 1])
            for line in file.readlines():
                if "q" == line[0]:
                    csvfile = re.findall('"([^"]*)"', line)
            file.close()

            csvfile = "games/{}".format(csvfile[0])
            cv = csv.reader(open(csvfile, "r"))
            for i, row in enumerate(cv):
                self.questions.append([row[0], [row[1], row[2]]])
                self.questions[i].append([row[3], row[4]])
                if row[5] != "":
                    self.questions[i].append([row[5], row[6]])
                if row[7] != "":
                    self.questions[i].append([row[7], row[8]])

            self.currentQuestion = self.questions[0]
            return self.currentQuestion[0]
